//
//  BMHUserPickerView.m
//  BMHVideoMain
//
//  Created by 爆米花 on 2017/4/25.
//  Copyright © 2017年 Maoyx. All rights reserved.
//

#import "BMHUserPickerView.h"
#import <ReactiveCocoa.h>

@interface BMHUserPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, assign) NSInteger areaId;
@property (nonatomic, copy)NSString  *proNameStr;


@end

@implementation BMHUserPickerView

-(RACSubject *)dismissSignal{
    if (_dismissSignal == nil) {
        _dismissSignal = [[RACSubject alloc]init];
    }
    return _dismissSignal;
}

-(RACSubject *)sureSignal{
    if (_sureSignal == nil) {
        _sureSignal = [[RACSubject alloc]init];
    }
    return _sureSignal;
}

- (void)setProTitleList:(NSArray *)proTitleList {
    _proTitleList = proTitleList;
    NSInteger row = [self.pickerView selectedRowInComponent:0];
    _proNameStr = [_proTitleList objectAtIndex:row];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = RGBA(0, 0, 0, 0);
        
        self.contentView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height, self.width, 250)];
        self.contentView.backgroundColor = RGB(237, 237, 237);
        [self addSubview:self.contentView];
        
        UIView *optionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.width, 44)];
        optionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:optionView];
        
        UIButton *cancleBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 88, optionView.height)];
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancleBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        cancleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        @weakify(self);
        [[cancleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self dismiss];
        }];
        [optionView addSubview:cancleBtn];
        
        UIButton *sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(optionView.width - cancleBtn.width, 0, cancleBtn.width, cancleBtn.height)];
        [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
        [sureBtn setTitleColor:RGB(111, 157, 245) forState:UIControlStateNormal];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            //NSString *address = [self getAddressWithAreaId:self.areaId == 0 ? 500 : self.areaId];
            [self.sureSignal sendNext:_proNameStr];
            [self dismiss];
        }];
        [optionView addSubview:sureBtn];
        
        self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, (self.contentView.height - 150 + 44) / 2, self.contentView.width, 150)];
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        self.pickerView.showsSelectionIndicator=YES;
        self.pickerView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.pickerView];
      
            }
    return self;
}


#pragma Mark - animate 视图操作
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self];
    if (point.y < self.contentView.top) {
        [self dismiss];
    }
}

- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = RGBA(0, 0, 0, 0.5);
        self.contentView.top = self.height - self.contentView.height;
    }];
}

- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = RGBA(0, 0, 0, 0);
        self.contentView.top = self.height;
    } completion:^(BOOL finished) {
        if (finished) {
            [self.dismissSignal sendNext:nil];
        }
    }];
}


#pragma Mark -- UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    //if (component == 0) {
        return [_proTitleList count];
    //}
    
    //return [_proTimeList count];
}

#pragma Mark -- UIPickerViewDelegate
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    if (component == 1) {
        return 40;
    }
    return 180;
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //if (component == 0) {
        _proNameStr = [_proTitleList objectAtIndex:row];
        NSLog(@"nameStr=%@",_proNameStr);
//    } else {
//        NSString  *_proTimeStr = [_proTimeList objectAtIndex:row];
//        NSLog(@"_proTimeStr=%@",_proTimeStr);
//    }
//    
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   // if (component == 0) {
        return [_proTitleList objectAtIndex:row];
//    } else {
//        return [_proTimeList objectAtIndex:row];
//        
//    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
